<?php

/**
 * 
 * ClientEngage: ClientEngage Visitor Voice (http://www.clientengage.com)
 * Copyright 2012, ClientEngage (http://www.clientengage.com)
 *
 * You must have purchased a valid license from CodeCanyon in order to have 
 * the permission to use this file.
 * 
 * You may only use this file according to the respective licensing terms 
 * you agreed to when purchasing this item on CodeCanyon.
 * 
 * 
 * 
 *
 * @author          ClientEngage <contact@clientengage.com>
 * @copyright       Copyright 2013, ClientEngage (http://www.clientengage.com)
 * @link            http://www.clientengage.com ClientEngage
 * @since           ClientEngage - Visitor Voice v 1.0
 * 
 */
App::uses('Controller', 'Controller');

/**
 * Application-wide controller
 */
class AppController extends Controller
{

    /**
     * Indicates whether a saving operation was blocked due to the system being in demo-mode
     * @var boolean
     */
    public static $demoBlocked = false;

    /**
     * Sets the application-wide default layout
     * @var string
     */
    public $layout = 'system';

    /**
     * Initialises the app-wide pagination-options array
     * @var array
     */
    public $paginate = array();

    /**
     * Contains the app-wide helpers array
     * @var array 
     */
    public $helpers = array(
        'Html' => array('className' => 'AppHtml'),
        'Form' => array('className' => 'AppForm'),
        'Session',
        'Time' => array('className' => 'AppTime'),
        'Text',
        'Layout',
        'Number' => array('className' => 'AppNumber'),
        'Customform'
    );

    /**
     * Contains the app-wide components array
     * @var array 
     */
    public $components = array(
        'Session',
        'App',
        'Auth' => array(
            'flash' => array(
                'key' => 'auth',
                'element' => Flash::Error,
                'params' => null
            ),
            'loginAction' => array(
                'controller' => 'users',
                'action' => 'login',
                'admin' => false
            ),
            'autoRedirect' => false,
            'loginRedirect' => array('controller' => 'visitorforms', 'action' => 'index', 'admin' => true),
            'authError' => 'Access not allowed.',
            'authenticate' => array(
                'Form' => array(
                    'fields' => array('username' => 'email'),
                    'scope' => array('User.active' => true)
                )
            ),
        ),
    );

    public function __construct($request = null, $response = null)
    {
        parent::__construct($request, $response);
        Configure::write('Config.language', AppConfig::read('System.language'));
        Configure::write('Config.timezone', AppConfig::read('System.timezone'));
    }

    /**
     * Application-wide pre-render processing
     */
    public function beforeRender()
    {
        if (self::$demoBlocked === true)
            $this->Session->setFlash(__('You cannot perform any changes in the demonstration system'));

        if (($flash = $this->Session->read('Message.flash')) !== null && $flash['element'] == 'default')
        {
            $flash['element'] = Flash::Warning;
            $this->Session->write('Message.flash', $flash);
        }

        if ($this->request->is('ajax'))
        {
            Configure::write('debug', 0);
            $this->layout = 'ajax';
        }
    }

    /**
     * AppController's beforeFilter()
     */
    public function beforeFilter()
    {
        $this->Auth->authError = __('Access not allowed.');

        if ($this->name == 'CakeError')
            $this->layout = 'default';

        if ($this->request->query('remove_frame') !== null)
        {
            CakeSession::write('Demo.remove_frame', true);
        }
    }

    public function beforeRedirect($url, $status = null, $exit = true)
    {
        if (self::$demoBlocked === true)
            $this->Session->setFlash(__('You cannot perform any changes in the demonstration system'));

        parent::beforeRedirect($url, $status, $exit);
    }

}